package com.boxuegu.controller;

import com.boxuegu.dao.LineDao;
import com.boxuegu.service.LineService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class LineController {
    Scanner sc = new Scanner(System.in);

    //程序开启欢迎界面
    public void start() throws IOException {

        lo:while(true){
            System.out.println("------------欢迎乘坐北京地铁------------");
            System.out.println("请输入您的选择: 1.查看路线站点列表    2.查询乘车路线    3.购票乘车  4.返回上一层");
            String choice = sc.next();
            switch(choice){
                case "1":
                    //查看路线站点列表
                    show();
                    break;
                case "2":
                    //查询乘车路线
                    LineService lineService = new LineService();
                    lineService.routeMap();
                    break;
                case "3":
                    //购票乘车
                    LineService lineService2 = new LineService();
                    lineService2.buyTicket();
                    break;

                case "4":
                    //退出系统
                    System.out.println("返回上一层");
                    break lo;
                default:
                    System.out.println("您输入的内容不存在, 请重新输入。");

            }
        }
    }

    //查看路线站点列表
    private void show(){
        while (true){
            System.out.println("请输入您需要查看的路线, 或输入0查看所有路线的站点:");
            String inputLine = sc.next();
            switch(inputLine){
                //根据选项查看站点信息
                case "0":
                    //展示所有线路
                    findAllRoutes();
                    //选择继续查看,或者选择乘车选项
                    choice();
                    break;
                case "1":
                    //展示1号线
                    findFirstRotes();
                    //选择继续查看,或者选择乘车选项
                    choice();
                    break;
                case "4":
                    //展示4号线
                    findFourRotes();
                    //选择继续查看,或者选择乘车选项
                    choice();
                    break;
                default:
                    System.out.println("线路不存在, 请重新输入。");
            }
        }
    }


//展示所有的路线
private void findAllRoutes() {
    LineService lineService = new LineService();
    ArrayList<Object> allRoutesList = lineService.findAllRoutes();
    for (Object s : allRoutesList) {
        System.out.println(s);
    }
}
    //展示1号线
    private void findFirstRotes() {
        LineService lineService = new LineService();
        ArrayList<Object> allRoutesList = lineService.findAllRoutes();

        for (int i = 0; i < 3; i++) {
            System.out.println(allRoutesList.get(i));
        }
    }
    //展示4号线
    private void findFourRotes() {
        LineService lineService = new LineService();
        ArrayList<Object> allRoutesList = lineService.findAllRoutes();
        for (int i = 3; i < allRoutesList.size(); i++) {

            System.out.println(allRoutesList.get(i));
        }
    }
    //选择继续查看, 或者选择乘车选项
    public void choice(){
        //选择继续查看,或者选择乘车选选项
        System.out.println("继续查看请按1, 选择乘车请按2:");
        while (true){
            String choice = sc.next();
            if(choice.equals("1")){
                show();
            }else if(choice.equals("2")){
                //选择乘车
                LineService lineService = new LineService();
                try {
                    lineService.buyTicket();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else{
                System.out.println("您的输入有误, 请重新输入。");
            }
        }

    }

}

